package com.max.back.common.excel;

import com.max.back.common.http.ResponseResult;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.List;

/**
 * BadRequestExceptionHandler
 */
@RestControllerAdvice
public class BadRequestExceptionHandler {
    /**
     * 参数校验统一返回
     * @author cc
     * @date 2021/7/3 14:40
     */
    @ExceptionHandler({MethodArgumentNotValidException.class})
    public ResponseResult parameterExceptionHandler(MethodArgumentNotValidException e){
        e.printStackTrace();
        StringBuffer msg = new StringBuffer();
        List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
        for (FieldError fieldError : fieldErrors) {
            msg.append(fieldError.getDefaultMessage()).append("\n");
        }
        return ResponseResult.getErrorResult(msg.toString());
    }
}
